package com.coc.proxy;

public class Proxy<T> implements Sourceable {
	
	//普通方式代理
	private Source source;	
	public Proxy(){
		super();
		this.source = new Source();
	}
	
	
	private T t;	
	//反射代理
	@SuppressWarnings("unchecked")
	public Proxy(T t) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
		super();
		this.t = (T) Class.forName(t.getClass().getName()).newInstance();
	}
	
	private Sourceable s;
	public Proxy(T t,int i) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		super();
		this.s = (Sourceable) Class.forName(t.getClass().getName()).newInstance();
	}
	
	

	//在代理的方法前后 增加操作
	public void method() {
		before();
		if(source != null)
			source.method();
		if(t != null)
			((Sourceable) t).method();
		if(s != null)
			s.method();
		after();
	}

	public void after(){
		System.out.println("after proxy .....\n");
	}
	
	public void before(){
		System.out.println("before proxy .....");
	}
}
